package com.xh.audit.controller.unituser;

import com.mybatisflex.core.paginate.Page;
import com.xh.audit.entity.unituser.Unit;
import com.xh.audit.service.UnitService;
import com.xh.audit.vo.webform.UnitQuery;
import com.xh.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 审计单位 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/unit")
@RequiredArgsConstructor
@Validated
public class UnitController {
    private final UnitService unitService;

    /**
     * @param tosave
     * @return
     */
    @PostMapping("/save")
    public R<Unit> save(@RequestBody
                        @Validated
                                Unit tosave) {
        boolean saveFlag = unitService.saveOrUpdate(tosave);

        if (saveFlag) {
            return R.success(unitService.getById(tosave.getId()));
        }
        return R.failed("保存失败");
    }

    @PostMapping("/queryByPage")
    public R<Page<Unit>> query(@RequestBody
                               @Validated
                                       UnitQuery queryPage) {
        Page<Unit> page1 = unitService.page(queryPage.getPageInfo(), queryPage.queryWrapperBuild());
        return R.success(page1);
    }

    @PostMapping("/info/{id}")
    public R<Unit> info(@PathVariable
                        @Validated
                        @NotNull(message = "id不能空")
                                Integer id) {

        Unit byId = unitService.getById(id);
        return R.success(byId);
    }

    @PostMapping("/del")
    public R<Unit> del(@RequestBody
                       @Validated
                       @NotEmpty(message = "ids 集合不能空")
                               List<Integer> ids) {
        unitService.removeByIds(ids);
        return R.success("删除成功");
    }
}
